package com.kt.common.clazz.pattern

import com.kt.common.clazz.interface1.extensionfun.println


fun main() {
    /*val box = Box<Int>(56)
    println(box.value)*/
    /*val from = arrayOf<Any>(1, 2, 3)
    val to = arrayOfNulls<Any>(3)
    copy(from = from, to = to)
    to.prinltAll()*/

    /*fill(Array<CharSequence>(3) { "1" }, "")
    copy1(Array<String>(3) { "1" })*/
    /*val val1:Int = 19
    println(val1.basicToString())*/
    //sort(listOf(1, 2, 3)) // OK。Int 是 Comparable<Int> 的子类型
    //sort(listOf(HashMap<Int, String>())) // 错误：HashMap<Int, String> 不是 Comparable<HashMap<Int, String>> 的子类型
    copyWhenGreater(mutableListOf<String>("2","3"),"sad").println()
}

fun demo(strs: Source<String>) {
    var objs: Source<Any> = strs
}

fun demo1(str: Comparable<String>) {
    var c = str
}

//此处out 指代 extend T ，用于生产者
//此处in 指代 super T ，用于消费者

/**
 * 此处是in 只能在参数里边消费，不可作为生产者
 */
interface Comparable2<in T> {

    fun compareTo(t: T): Int
}

/**
 * 此处是out只能作为生产者
 */
interface Comparable1<out T> {

    fun compareTo(): T
}

/**
 *
 */
/*class Array<T>(val size: Int) {
    fun get(position: Int): T {
        return null
    }

    fun set(t: T) {

    }
}*/

fun copy(from: Array<Any>, to: Array<Any?>) {
    assert(from.size == to.size)
    for (i in from.indices)
        to[i] = from[i]
}

fun Array<Any?>.prinltAll() {
    if (this.isNullOrEmpty()) return
    this.forEach {
        println(it ?: "")
    }
}

//类型投影  in -> Array<? super String> 逆变类型参数
fun fill(dest: Array<in String>, value: String) {

}

//  out -> Array<? extends String> 协变类型参数
fun copy1(from: Array<out Any>) {

}

open interface E
open class F : E

class Foo<in T : F> {
//    fun setA(t: T) {}
}

/*
fun <T>singleListItem(item:T) :List<T>{

}*/

fun <T> T.basicToString(): String {
    return this.toString()
}


fun <T : kotlin.Comparable<T>> sort(list: List<T>) {}

fun <T> copyWhenGreater(list: MutableList<T>, threshold: T): MutableList<T> where T : CharSequence,
                                                                           T : Comparable<T> {
    list.add(threshold)
    return list
}

/*
fun <T>MutableList<T>.println(){
    this.forEach{
        println(it)
    }
}*/
